package com.run.cat.api.controller;


import com.run.cat.common.base.BaseResult;
import com.run.cat.common.util.EmptyUtil;
import com.run.cat.dao.entity.Attention;
import com.run.cat.dao.entity.User;
import com.run.cat.service.IAttentionService;
import com.run.cat.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 关注的人关系表 前端控制器
 * </p>
 *
 * @author CG
 * @since 2020-08-19
 */
@Controller
@RequestMapping("/attention")
public class AttentionController {

    @Autowired
    private IAttentionService attentionService;

    @Autowired
    private IUserService userService;

    @RequestMapping("add")
    @ResponseBody
    public BaseResult add(@RequestBody Attention attention){
        attentionService.insert(attention);
        User attentionUser = userService.selectById(attention.getAttentionUserId());
        if(EmptyUtil.isNotEmpty(attentionUser)) {
            attentionUser.setAttention(attentionUser.getAttention() + 1);
            userService.updateById(attentionUser);
        }
        return BaseResult.success();
    }


    @RequestMapping("/clean")
    @ResponseBody
    public BaseResult clean(@RequestBody Attention attention){
        User attentionUser = userService.selectById(attention.getAttentionUserId());
        if(EmptyUtil.isNotEmpty(attentionUser)) {
            attentionUser.setAttention(attentionUser.getAttention()-1 > 0 ? 0 :attentionUser.getAttention() - 1);
            userService.updateById(attentionUser);
        }
        attentionService.deleteById(attention.getId());
        return BaseResult.success();
    }

}
